package com.hqyj.shop.service.imp;


import com.hqyj.shop.dao.UserDao;
import com.hqyj.shop.dao.UserMutilDao;
import com.hqyj.shop.pojo.Manager;
import com.hqyj.shop.pojo.User;
import com.hqyj.shop.service.userService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements userService {
   @Autowired
   private UserDao userDao;
    @Autowired
    private UserMutilDao userMutilDao;

//注册用户
    @Override
    public boolean registerUser(User user) {
        userDao.registerUser(user);
        return true;
    }

    @Override
    public User getUserByUserName(String username) {
        return userMutilDao.queryByUsername(username);
    }

    @Override
    public User getOneByUserName(String username) {

        return userDao.queryByUsername(username);
    }

    @Override
    public Map<String, Object> login(String username, String password) {
        Map<String,Object> result = new HashMap<>();
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();//查询数据库？
        System.out.println(username);
        System.out.println(password);
        //判断当前用户是否已经登陆过
        if(!subject.isAuthenticated()){//没有登陆过
            //登录
            //创建认证令牌
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);

            try {
                subject.login(token);//这里委托securityManager再委托调用dogetauthencationinfo方法认证带着token里面有账号密码
            }catch (UnknownAccountException e){
                //未知账号异常
                result.put("code",2);
                result.put("message",username+"用户名不存在");
                return result;
            }catch (IncorrectCredentialsException e){
                //密码错误异常
                result.put("code",3);
                result.put("message",username+"用户密码错误");
                return  result;
            }catch (AuthenticationException e){
                result.put("code",4);
                result.put("message","认证失败");
                return  result;
            }
        }
        //认证通过,获取以保存在shiro session域中的用户信息
        Session session = subject.getSession();
        Object loginUser = session.getAttribute("loginUser");
        result.put("code",5);
        result.put("message","认证成功");
        //返回给控制器
        result.put("loginUser",loginUser);


        return result;
    }
//通过账号密码查询管理员
    @Override
    public Manager queryManagerByUsernameAndPassword(String username, String password) {
        return userDao.queryManagerByUsernameAndPassword(username,password);
    }


    //忘记密码
    @Override
    public User queryUserByFor(String user_account, String phone) {
        return userDao.queryUserByFor(user_account,phone);
    }
    //通过账号和电话修该密码
    @Override
    public void updatePassword(String user_account, String phone,String password) {
        userDao.updatePassword(user_account, phone,password);
    }

    //通过账号密码查询用户
    @Override
    public User queryUserByUsernameAndPassword(String username,String password){
        return userDao.queryUserByUsernameAndPassword(username,password);
    }
    @Override
    public Map<String, Object> login1(String username, String password) {
        System.out.println("123");
        Map<String,Object> result = new HashMap<>();
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();//查询数据库？

        //判断当前用户是否已经登陆过
        if(!subject.isAuthenticated()){//没有登陆过
            //登录
            //创建认证令牌
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);

            try {
                subject.login(token);//这里委托securityManager再委托调用dogetauthencationinfo方法认证带着token里面有账号密码
            }catch (UnknownAccountException e){
                //未知账号异常
                result.put("code",2);
                result.put("message",username+"用户名不存在");
                return result;
            }catch (IncorrectCredentialsException e){
                //密码错误异常
                result.put("code",3);
                result.put("message",username+"用户密码错误");
                return  result;
            }catch (AuthenticationException e){
                result.put("code",4);
                result.put("message","认证失败");
                return  result;
            }
        }
        //认证通过,获取以保存在shiro session域中的用户信息

        Session session = subject.getSession();
        Object loginUser = session.getAttribute("loginUser");
        System.out.println(loginUser);
        result.put("code",5);
        result.put("message","认证成功");
        //返回给控制器
        result.put("loginUser",loginUser);


        return result;
    }
    public Map<String, Object> logout() {
        //使用shiro框架获取当前用户
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","成功登出");

        return result;
    }
}
